package com.czq.gulimail.gulimailproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czq.common.utils.PageUtils;
import com.czq.common.utils.Query;
import com.czq.gulimail.gulimailproduct.dao.CategoryBrandRelationDao;
import com.czq.gulimail.gulimailproduct.entity.CategoryBrandRelationEntity;
import com.czq.gulimail.gulimailproduct.service.CategoryBrandRelationService;
import com.czq.gulimail.gulimailproduct.vo.resp.BrandCategoryResp;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> getListByBrandId(Long id) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", id);
        return this.list(wrapper);
    }

    @Override
    public void deleteByBrandIdAndCateId(CategoryBrandRelationEntity categoryBrandRelationEntity) {

        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", categoryBrandRelationEntity.getBrandId()).eq("catelog_id", categoryBrandRelationEntity.getCatelogId());
        this.remove(wrapper);
    }

    @Override
    public void deleteBatchByBrandId(Long[] ids) {
        if (ids == null || ids.length == 0) {
            // 如果数组为空，则直接返回，避免不必要的数据库操作
            return;
        }

        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.in("brand_id", ids);
        this.remove(wrapper);
    }

    @Override
    public void deleteBatchByCateId(Long[] ids) {
        if (ids == null || ids.length == 0) {
            // 如果数组为空，则直接返回，避免不必要的数据库操作
            return;
        }

        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.in("catelog_id", ids);
        this.remove(wrapper);
    }

    @Override
    public void updateByBrand(Long brandId, String name) {
        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("brand_id", brandId);
        wrapper.set("brand_name", name);
        this.update(wrapper);
    }

    @Override
    public void updateByCate(Long catId, String name) {
        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("catelog_id", catId);
        wrapper.set("catelog_name", name);
        this.update(wrapper);
    }

    @Override
    public List<BrandCategoryResp> getBrandList(Long catId) {
        List<BrandCategoryResp> list = new ArrayList<>();
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id", catId);
        List<CategoryBrandRelationEntity> list1 = this.list(wrapper);
        for (CategoryBrandRelationEntity categoryBrandRelationEntity : list1) {
            BrandCategoryResp brandCategoryResp = new BrandCategoryResp();
            BeanUtils.copyProperties(categoryBrandRelationEntity, brandCategoryResp);
            list.add(brandCategoryResp);
        }
        return list;
    }

}
